import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:web_socket_channel/io.dart';
import 'dart:ui';
import 'package:dio/dio.dart';
// 自定义模块
import '../module/my_appbar.dart';
import '../module/my_socket.dart';
import '../components/user_item.dart';
import '../module/custome_router.dart';
// import '../module/my_socket.dart';

class AllUserList extends StatefulWidget {
  _AllUserListState createState() => _AllUserListState();
}

class _AllUserListState extends State<AllUserList> with AutomaticKeepAliveClientMixin {
  String _title = '联 系 人';
  List userList = [];

  @override
  void initState(){
    _title = _title;
    userList = userList;
    if (userList.length <= 0) {
      getAllUser();
    }
    openSocket();
    super.initState();
  }
  @override
  bool get wantKeepAlive => true;
  
  void openSocket () {
  }

  void getAllUser () {
    Dio().get(
      'https://hinode.shangning.site/api/csuser/getalluser',
      data: {
        'butt_id' : 1138
      }
    ).then((response) {
      if (response.data['status'] == 1) {
        setState(() {
          userList =  response.data['msg'];
        });
      }
    });
  }

  Widget renderList () {
    if (userList.length > 0) {
      return ListView.builder(
        padding: EdgeInsets.all(0),
        itemCount: userList.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            child: UserItem(
              userItem: userList[index],
              tapStatus: userList[index]['tap']
            ),
            onTapDown: (TapDownDetails details) {
              setState((){userList[index]['tap'] = true;});
            },
            onTapUp: (TapUpDetails details) {
              setState((){userList[index]['tap'] = false;});
              // Navigator.of(context).push(CustomeRouter(
              //   UserChat(
              //     userId: 15,
              //   )
              // ));
            },
            onTap: (){
              print('测试1 ${ScreenUtil().setWidth(32)}');
              MySocket().onmsg('hello', (str){
                print('子页面收到消息');
              });

              print('测试2 ');
              // ${MySocket().setWidth(32)}
              
            },
            onTapCancel: () {
              setState((){userList[index]['tap'] = false;});
            },
          );
        },
      );
    } else {
      return Center(
        child: Text('加载中...'),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          MyAppBar(
            title: _title,
            textColor: Colors.white,
            leftICon: IconButton(
              tooltip: '打开客服界面',
              icon: Icon(
                IconData(0xe7f4, fontFamily: 'iconfont'),
                size: ScreenUtil().setWidth(32),
              ),
              color: Colors.white,
              onPressed: () {
                Scaffold.of(context).openDrawer();
              }
            ),
            rightICon: IconButton(
              tooltip: '问题反馈',
              icon: Icon(
                IconData(0xe645, fontFamily: 'iconfont'),
                size: ScreenUtil().setWidth(34),
              ),
              color: Colors.white,
              onPressed: () {
              }
            ),
          ),
          Expanded(
            child: Container(
              child: renderList(),
              color: Colors.white,
            )
          )
        ],
      ),
    );
  }
}